package org.jetnuke.modules.pictures.utils;

import java.io.StringWriter;
import java.util.ArrayList;
import java.util.List;

import net.jforum.dao.UserDAO;
import net.jforum.entities.Theme;
import net.jforum.entities.User;

import org.apache.log4j.Logger;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
import org.jetnuke.common.BlockInfo;
import org.jetnuke.common.IBlockInfo;
import org.jetnuke.dao.BlogCategoryDAO;
import org.jetnuke.dao.BlogUserSettingsDAO;
import org.jetnuke.dao.DataAccessDriver;
import org.jetnuke.dao.PictureAlbumDAO;
import org.jetnuke.dao.PictureUserSettingsDAO;
import org.jetnuke.entities.BlogUserSettings;
import org.jetnuke.entities.PictureAlbum;
import org.jetnuke.entities.PictureUserSettings;

public class PictureUtils {
	private final static Logger s_log = Logger.getLogger(PictureUtils.class);
	private PictureUtils(){}
	
	public static boolean isPictureInitialized(int uid)throws Exception{
		PictureUserSettingsDAO pusd=DataAccessDriver.getInst().newPictureUserSettingsDAO();
		PictureUserSettings bus=pusd.retrieveByPk(uid);
		if(bus.getID()==0)
			return false;
		return true;
	}
	public static void manageStyleAndBlocks(VelocityContext vc, int uid) throws Exception {
		PictureUserSettings pus=DataAccessDriver.getInst().newPictureUserSettingsDAO().retrieveByPk(uid);
		vc.put("settings",pus);
		List preLeftBlocks=new ArrayList();
		PictureUtils.manageStyle(pus,vc,preLeftBlocks);
		UserDAO um=DataAccessDriver.getInst().newUserDAO();
		net.jforum.entities.User user=um.selectById(uid);
		vc.put("user",user);
		List albums=DataAccessDriver.getInst().newPictureAlbumDAO().selectByUser(uid);
		vc.put("user_albums",albums);
		vc.put("_pagetitle"," - "+pus.getTITLE());
		
		preLeftBlocks.add(PictureUtils.createUserBlockInfo(vc,user));
	}
	public static void initializePictures(int uid) throws Exception{
		
		
		PictureAlbumDAO pad=DataAccessDriver.getInst().newPictureAlbumDAO();
		List ls=pad.selectByUser(uid);
		if(ls.isEmpty()){
			PictureAlbum pa=new PictureAlbum();
			pa.setUID(uid);
			pa.setTITLE("Untitled Album");
			pa.setDESCRIPTION("Untitled Album");
			pa.setKEYWORDS("");
			pad.addNew(pa);
		}
		
		PictureUserSettingsDAO pusd=DataAccessDriver.getInst().newPictureUserSettingsDAO();
		PictureUserSettings pus=pusd.retrieveByPk(uid);
		if(pus.getID()==0){
			pus.setID(uid);
			User user=DataAccessDriver.getInst().newUserDAO().selectById(uid);
			pus.setTITLE(user.getUsername()+"'s Pictures");
			pus.setDESCRIPTION("");
//			bus.setMAXNUMBERPOST(10);
			int theme=user.getThemeId();
			if(theme==0)
				theme=1;
			pus.setTHEME(theme);
			pusd.insert(pus);
			
		}
		
	}

	public static void manageStyle(PictureUserSettings pus, VelocityContext vc, List preLeftBlocks) {
		String _styleFolder = "_styleFolder";
		String currentStyle=null;
		try {
			Theme tm=net.jforum.dao.DataAccessDriver.getInstance().newThemeDAO().selectByID(pus.getTHEME());
			currentStyle = tm.getStyleName();
		} catch (Throwable e) {
				
		}
		if(currentStyle!=null)
			vc.put(_styleFolder, "style/" + currentStyle);
		vc.put("_currentStyle",currentStyle);
		vc.put("_preBlocks",preLeftBlocks);
		vc.put("_pagetitle"," - "+pus.getTITLE());
	}
	public static IBlockInfo createUserBlockInfo(VelocityContext vc, User us){
		StringWriter sw=new StringWriter();
		try {
			BlogUserSettingsDAO busm=DataAccessDriver.getInst().newBlogUserSettingsDAO();
			BlogUserSettings bus=busm.retrieveByPk(us.getId());
			if(bus.getID()!=0){
				vc.put("hasBlog",Boolean.TRUE);
				vc.put("blogTitle",bus.getTITLE());
			}
		
		
			Template t = Velocity.getTemplate("/modules/pictures/user_block.vm");
			vc.put("user",us);
			BlogCategoryDAO bcm=DataAccessDriver.getInst().newBlogCategoryDAO();
			List ls=bcm.selectByUser(us.getId());
			vc.put("blogCategories",ls);
			t.merge(vc, sw);
		} catch (Exception e) {
			s_log.error(e);
			
		}
		String title=(String)vc.get("_THIS_PICTURES");
		IBlockInfo bInfo=createBlockInfo(title,sw.toString());
		return bInfo;
	}
	private static IBlockInfo createBlockInfo(String title, String content){
		BlockInfo bInfo=new BlockInfo();
		
		bInfo.setPath("");
		bInfo.setTitle(title);
		bInfo.setAdminOnly(false);
		bInfo.setAnonymousOnly(false);
		bInfo.setBlockClass("org.jetnuke.common.BasicBlock");
		bInfo.setContent(content);
		bInfo.setPosition("l");
		bInfo.setValid(true);
		bInfo.setActivated(true);
		return bInfo;
	}
}
